/**  
* @Title: BaseCtrl.java 
* @Description:
* @Copyright: Copyright (c) 2018
* @Company:http://www.sinocon.cn
* @author Administrator  
* @date 2018年4月19日  
* @version 1.0  
*/
package com.sansui.admin.common.ctrl;

import java.beans.PropertyEditorSupport;
import java.sql.Timestamp;
import java.util.Date;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import cn.hutool.core.date.DateUtil;

/**
 * @Title: BaseCtrl
 * @Description:
 * @author:Administrator
 * @date 2018年4月19日
 */
public class BaseCtrl {
	protected final String _DICT_ROUTE = "/system/dict";
	protected final String _RESOURCES_ROUTE = "/system/resources";
	protected final String _ROLE_ROUTE = "/system/role";
	protected final String _USER_ROUTE = "/system/user";
	protected final static String ERROR_PATH = "/error";

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		// String类型转换，将所有传递进来的String进行HTML编码，防止XSS攻击
		binder.registerCustomEditor(String.class, new PropertyEditorSupport() {
			@Override
			public void setAsText(String text) {
				setValue(text == null ? null : StringEscapeUtils.escapeHtml4(text.trim()));
			}

			@Override
			public String getAsText() {
				Object value = getValue();
				return value != null ? value.toString() : "";
			}
		});

		// Date 类型转换
		binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
			@Override
			public void setAsText(String text) {
				setValue(DateUtil.parse(text));
			}
		});

		// Timestamp 类型转换
		binder.registerCustomEditor(Timestamp.class, new PropertyEditorSupport() {
			@Override
			public void setAsText(String text) {
				Date date = DateUtil.parse(text);
				setValue(date == null ? null : new Timestamp(date.getTime()));
			}
		});
	}

}
